﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MBC.UI {
	public partial class ImageList : UserControl {
		#region Dependency Properties

		internal ImageListModel imageListDataContext = new ImageListModel();

		public int LoanID {
			get { return (int)GetValue(LoanIDProperty); }
			set { SetValue(LoanIDProperty, value); }
		}

		// Using a DependencyProperty as the backing store for LoanID.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty LoanIDProperty =
				DependencyProperty.Register("LoanID", typeof(int), typeof(ImageList), new PropertyMetadata(LoanIDChangedCallback));

		static void LoanIDChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			((ImageList)d).imageListDataContext.LoanID = (int)e.NewValue;
		}

		#endregion
		public ImageList() {
			InitializeComponent();
			LayoutRoot.DataContext = imageListDataContext;
		}

	}
	#region Model
	public class ImageListModel : MBC.ServiceModel {
		const string SHOW_IMAGES_URL = "/manager/common/ShowApplImageFolder.aspx?pdf=pdf.pdf&t={0}&ApplID={1}";
		const string SHOW_FOLDER_URL = SHOW_IMAGES_URL+"&FolderID={2}";
		string GetShowImagesUrl(int loanID) { return string.Format(SHOW_IMAGES_URL,DateTime.Now.Ticks, LoanID); }
		string GetShowImagesUrl(byte folferID, int loanID) { return string.Format(SHOW_FOLDER_URL, DateTime.Now.Ticks, loanID, folferID); }
		#region Properties
		#region LoanID
		public override int LoanID {
			get { return base.LoanID; }
			set {
				if (base.LoanID == value) return;
				base.LoanID = value;
				RaisePropertyChanged("ShowAllFolders", "ShowAllFoldersUrl");
				ServiceClient.PageCountsAsync(LoanID,true,LoanID);
				Images.Clear();
				IsListLoading = true;
			}
		}
		#endregion
		#region IsListLoading
		bool isListLoading;
		public bool IsListLoading {
			get { return isListLoading; }
			set {
				if (isListLoading == value) return;
				isListLoading = value;
				RaisePropertyChanged("IsListLoading");
			}
		}
		#endregion
		public string ShowAllFolders { get { return "All Folders in " + LoanID; } }
		public string ShowAllFoldersUrl { get { return GetShowImagesUrl(LoanID); } }
		#region Images
		private ObservableCollection<ImageTypeInfo> images = new ObservableCollection<ImageTypeInfo>();

		public ObservableCollection<ImageTypeInfo> Images {
			get { return images; }
			set { images = value; }
		}
		#endregion
		#endregion
		public ImageListModel() {
			ServiceClient.PageCountsCompleted += new EventHandler<MBC.Service.PageCountsCompletedEventArgs>(ServiceClient_PageCountsCompleted);
		}

		void ServiceClient_PageCountsCompleted(object sender, MBC.Service.PageCountsCompletedEventArgs e) {
			if (LoanID != (int)e.UserState) return;
			IsListLoading = false;
			e.Result.ToList().ForEach(i => Images.Add(new ImageTypeInfo(i.ImageType + "," + i.Count, GetShowImagesUrl((byte)i.ID, LoanID))));
		}
	}
	public class ImageTypeInfo {
		public string Type { get; set; }
		public string Url { get; set; }
		public ImageTypeInfo(string type,string url) {
			this.Type = type;
			this.Url = url;
		}
	}
	#endregion
}
